//
//  MSWeaponNode.m
//  MagicSword
//
//  Created by Netease on 15/11/19.
//  Copyright © 2015年 Jonear. All rights reserved.
//

#import "MSWeaponNode.h"

#define NodeSpace 20
@implementation MSWeaponNode {
    MSWeaponInfo *_weaponInfo;
    SKSpriteNode *_avatarNode;
    SKSpriteNode *_backgroundNode;
    SKShapeNode  *_shapeNode;
    SKLabelNode  *_userCountLabel;
    SKTextureAtlas *_atlas;
}

- (id)init {
    self = [super initWithColor:[UIColor clearColor] size:CGSizeZero];
    if (self) {
        float size = 50;
        self.size = CGSizeMake(size, size);
        self.zPosition = 3;
        
        _atlas = [SKTextureAtlas atlasNamed:@"weapons"];
        
        _avatarNode = [[SKSpriteNode alloc] init];
        _avatarNode.zPosition = 4;
        _avatarNode.size = CGSizeMake(size-NodeSpace, size-NodeSpace);
        _avatarNode.position = CGPointMake(0, 0);
        [self addChild:_avatarNode];
        
        _userCountLabel = [SKLabelNode labelNodeWithFontNamed:@"Helvetica"];
        _userCountLabel.zPosition = 5;
        _userCountLabel.position = CGPointMake(size/2-8, -size/2);
        _userCountLabel.fontSize = 11;
        _userCountLabel.fontColor = [UIColor purpleColor];
        [self addChild:_userCountLabel];
        
        _backgroundNode = [SKSpriteNode spriteNodeWithImageNamed:@"avatar_background"];
        _backgroundNode.zPosition = 3;
        _backgroundNode.size = CGSizeMake(size, size);
        _backgroundNode.position = CGPointMake(0, 0);
        _backgroundNode.hidden = YES;
        [self addChild:_backgroundNode];
        
        CGRect circle = CGRectMake(-size/2, -size/2, 40.0, 40.0);
        _shapeNode = [[SKShapeNode alloc] init];
        _shapeNode.path = [UIBezierPath bezierPathWithOvalInRect:circle].CGPath;
        _shapeNode.strokeColor = _shapeNode.fillColor = UIColorFromRGBA(0xd6cb89, 0.7);
        _shapeNode.lineWidth = 1;
        _shapeNode.hidden = YES;
        [self addChild:_shapeNode];
    }
    return self;
}

- (id)initWithWeaponInfo:(MSWeaponInfo *)info {
    self = [self init];
    if (self) {
        [self setWeaponInfo:info];
    }
    return self;
}

- (void)setWeaponInfo:(MSWeaponInfo *)info {
    _weaponInfo = info;
    
    SKTexture *texture = [_atlas textureNamed:info.imageName];
    [_avatarNode setTexture:texture];
    
    _userCountLabel.text = [NSString stringWithFormat:@"%d", info.useCount];
}

- (void)setSize:(CGSize)size {
    [super setSize:size];
    [_backgroundNode setSize:size];
    float space = NodeSpace/50. * size.width;
    _avatarNode.size = CGSizeMake(size.width-space, size.height-space);
    
    _userCountLabel.position = CGPointMake(size.width/2-6, -size.height/2);
    
    CGRect circle = CGRectMake(-size.width/2-3, -size.height/2-3, size.width+6, size.height+6);
    _shapeNode.path = [UIBezierPath bezierPathWithOvalInRect:circle].CGPath;
}

- (void)setName:(NSString *)name {
    [super setName:name];
    [_avatarNode setName:name];
    [_backgroundNode setName:name];
    [_userCountLabel setName:name];
    [_shapeNode setName:name];
}

- (void)setBackgroundHidden:(BOOL)ishidden {
    _backgroundNode.hidden = ishidden;
}

- (void)setShapeNodeHidden:(BOOL)ishidden {
    _shapeNode.hidden = ishidden;
}

- (void)setCountTitleHidden:(BOOL)ishidden {
    _userCountLabel.hidden = ishidden;
}

- (void)setCountTitleColor:(UIColor *)color {
    [_userCountLabel setFontColor:color];
}

- (MSWeaponInfo *)weaponInfo {
    return _weaponInfo;
}

@end
